package io.renren.common.utils;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import org.springframework.web.multipart.MultipartFile;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

public class CsvtojsonUtils {

    //csv->json->list<类>
    public static List<String> readLogCsvToList(MultipartFile file){
        List<String> list = new ArrayList<String>();

        if (!file.isEmpty()){
            InputStreamReader isr = null;
            BufferedReader br = null;
            try {
                isr = new InputStreamReader(file.getInputStream());
                br = new BufferedReader(isr);
                String line = null;
                List<List<String>> strs = new ArrayList<List<String>>();
                while ((line = br.readLine()) != null){
                    strs.add(Arrays.asList(line.split(",")));
                }
                JSONArray array = toJsonArray(strs);
                list = array.toJavaList(String.class);
            } catch (IOException e) {
                //
            }finally {
                try {
                    if (br != null){
                        br.close();
                    }
                    if (isr != null){
                        isr.close();
                    }
                } catch (IOException e) {
                    //
                }
            }
        }else {
            //
        }
        return list;
    }

    //csv->json<类>
    public static JSONArray readLogCsvToJsonArr(MultipartFile file){
        List<String> list = new ArrayList<String>();
        JSONArray array = new JSONArray();
        if (!file.isEmpty()){
            InputStreamReader isr = null;
            BufferedReader br = null;
            try {
                isr = new InputStreamReader(file.getInputStream());
                br = new BufferedReader(isr);
                String line = null;
                List<List<String>> strs = new ArrayList<List<String>>();
                while ((line = br.readLine()) != null){
                    strs.add(Arrays.asList(line.split(",")));
                }
                 array = toJsonArray(strs);
//                list = array.toJavaList(String.class);
            } catch (IOException e) {
                //
            }finally {
                try {
                    if (br != null){
                        br.close();
                    }
                    if (isr != null){
                        isr.close();
                    }
                } catch (IOException e) {
                    //
                }
            }
        }else {
            //
        }
        return array;
    }

    //fastjson
    public static JSONArray toJsonArray(List<List<String>> strs){
        JSONArray array = new JSONArray();
        for (int i = 1; i < strs.size(); i++) {
            JSONObject object = new JSONObject();
            for (int j = 0; j < strs.get(0).size(); j++) {
                object.put(strs.get(0).get(j),strs.get(i).get(j));
            }
            array.add(object);
        }
        return array;
    }
}
